package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class RegionServiceImpl implements IRegionService {
    @Autowired
    private RegionRepository regionRepository;
    //
    //因为加了这个依赖spring-boot-starter-data-mongodb,
    // 就自动装配注解扫描后.最后会生成mongoTemplate对象,放在spring容器给我们用
    @Autowired
    private MongoTemplate mongoTemplate;//可以操作数据库

    @Override
    public void save(Region region) {
        region.setId(null);
        regionRepository.save(region);
    }

    @Override
    public void update(Region region) {
        regionRepository.save(region);
    }

    @Override
    public Region get(String id) {
        Optional byId = regionRepository.findById(id);
//强转
        return (Region) byId.orElse(null);//如果是空是就返回空值,否则返回对象
    }

    @Override
    public List<Region> listAll() {
        List<Region> regions = regionRepository.findAll();
        return regions;
    }

    @Override
    public void delete(String id) {
        regionRepository.deleteById(id);
    }


    /**
     * 抽取的新增/修改方法
     * @param region
     */
    @Override
    public void saveOrUpdate(Region region) {
        if (!StringUtils.hasLength(region.getId())) {
            this.save(region);
        } else {
//            //修改操作
            this.update(region);
//            这个是修改数据库区域表全部字段的值,可以不用使用全量更新
//            //package org.springframework.data.mongodb.core.query;
//            //是这个包
//            Query query = new Query();
//            //先查询用户点击的这个对象,再调用修改方法做修改  避免全量修改掉
//            query.addCriteria(Criteria.where("_id").is(region.getId()));
            //更新
            //方案1: 1:查询  2: 替代  3:save
            //方案2: 显示使用update逻辑 update set...
            //条件

           /*
            //更新的列
            Update update = new Update();
            update.set("name",region.getName());
            update.set("sn",region.getSn());
            update.set("refIds",region.getRefIds());
            update.set("ishot",region.getIshot());
            update.set("sequence",region.getSequence());
            update.set("info",region.getInfo());
            //执行更新
            template.updateMulti(query, update, Region.class);*/
//           方式二:
//            DBHelper.update(template, query, region, "name","sn","refIds","ishot","sequence","info");

        }
    }

    @Override
    public Page<Region> query(RegionQuery qo) {
        //查询对象
        Query query = new Query();
        //查询出总数条数
        long rows = mongoTemplate.count(query, Region.class);
        if (rows == 0) {
            return  Page.empty();//如果总数条数是0,就返回空集合
        }
        //分页对象(设置当前页从0开始算,还有页面显示条数,按sequence这个列正序排列 )    Sort.Direction.ASC,表示按正序排列表格
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "sequence");
        //给查询对象
        query.with(pageable);
        List<Region> list = mongoTemplate.find(query, Region.class);

        return  new PageImpl<Region>(list,pageable,rows);//相当于pageResult对象

    }
//热门区域
    @Override
    public List<Region> hotRegion() {
        //查询出状态是火热的区域对象集合,同时按序号字段排序.最后返回前端
        List<Region> regions = regionRepository.findByIshotOrderBySequence(Region.STATE_HOT);
        return regions;
    }
}
